package com.nineclock.document.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.entity.Result;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocFileHistoryMapper;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocFileHistoryService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DocFileHistoryServiceImpl implements DocFileHistoryService {

    @Autowired
    private DocFileHistoryMapper docFileHistoryMapper;

    @Autowired
    private SysCompanyUserFeign sysCompanyUserFeign;

    //查询文件历史记录
    @Override
    public List<DocFileHistoryDto> queryHistory(Long fileId) {
        LambdaQueryWrapper<DocFileHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFileHistory::getFileId,fileId);
        List<DocFileHistory> docFileHistories = docFileHistoryMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(docFileHistories)){
            //转换
            List<DocFileHistoryDto> dtoList = BeanHelper.copyWithCollection(docFileHistories, DocFileHistoryDto.class);
            for (DocFileHistory docFileHistory : docFileHistories) {
                Result<SysCompanyUserDTO> sysCompanyUserDTOResult = sysCompanyUserFeign.queryCompanyUserById(docFileHistory.getOperateUserId());
                for (DocFileHistoryDto docFileHistoryDto : dtoList) {
                    docFileHistoryDto.setUserName(sysCompanyUserDTOResult.getData().getUserName());
                }
            }

            return dtoList;
        }
        return null;
    }
}
